//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2006
//
// 'Postal address items' dialog, 'Regions' page.
//

# include "StdAfx.h"
# include "Resource.h"
# include "PAItems.h"
# include "ListView.h"
# include "Map.h"
# include "ChangePAI.h"
# include "EditText.h"
# include "PropertyPages.h"
# include "Globals.h"

void InitRegionsList (CListView & _List) {
	AddColumn (_List, 0, "Region/Province");
	AddColumn (_List, 1, "Code");
	AddColumn (_List, 2, "Country");

	_List.SetExtStyle (LVS_EX_FULLROWSELECT);
}

void FillRegionsList (CListView & _List, const region_t * _pSelectRegion) {
	ListView_DeleteAllItems (_List);

	CCritSec::CLock lock (g_cs);

	size_t cItem = 0;
	for (regions_t::iterator i = g_map.Regions.begin (); i != g_map.Regions.end (); ++ i) {
		const region_t & region = * i;
		if (region.IsDeleted ())
			continue;

		InsertItem     (_List, cItem, reinterpret_cast<LPARAM> (& region), "%s", OutputPAString (region.strName));
		SetSubItemText (_List, cItem, 1, "%s", OutputPAString (region.strAbbrevName));
		SetSubItemText (_List, cItem, 2, "%s", OutputPAString (region.pCountry->strName));

		if (& region == _pSelectRegion)
			SetSelection (_List, cItem);

		++ cItem;
	}

	// Set the best width.
	AutoSizeColumns (_List);

	_List.Sort ();

//	if (g_map.Countries.empty ())
//		::EnableWindow (_hList, FALSE);
}

/////////////////////////////////////////////////////

class CCreateRegion : public CDialog {
  public:
	CCreateRegion () : CDialog (IDD_CREATE_PAI) {}

	// Override.
	virtual bool OnInit (HWND _hwndFocus) {
		SetWindowText (m_hWnd, "Add Region");

		EnableDlgItem (IDOK, false);

		// Static.
		SetDlgItemText (IDC_PARENT_ITEMS, "Countries:");

		// Countries.
		m_List.Init (m_hWnd, IDC_LIST, "Countries");
		InitCountriesList (m_List);
		FillCountriesList (m_List);
		SetSelection (m_List, 0);

		return true;
	}
	virtual void OnDestroy () {
		m_List.Destroy ();
	}

	virtual void OnCommand (WORD _wCmd, WORD _wNotifyCode, HWND _hwndCtl) {
		switch (_wCmd) {
			case IDC_TEXT: {
				if (_wNotifyCode == EN_CHANGE)
					EnableDlgItem (IDOK, true);
				return;
			}

			case IDOK: {
				const HWND hText = GetDlgItem (IDC_TEXT);

				// Get text.
				const size_t cLen = ::GetWindowTextLength (hText);
				char * const strName = reinterpret_cast<char *> (alloca (cLen + 1));
				::GetWindowText (hText, strName, cLen + 1);

				// Get country.
				const size_t cSel = ListView_GetSelectionMark (m_List);
				if (cSel == -1)
					break;
				country_t * const pCountry = reinterpret_cast<country_t *> (GetLParam (m_List, cSel));

				// Create new region.
				AddRegion (strName, pCountry);

				EndDialog (0);
				return;
			}

			case IDCANCEL:
				EndDialog (1);
				return;
		}
	}

	virtual bool OnNotify (int _idCtrl, NMHDR * _pInfo) {
		switch (_pInfo->code) {
			case NM_RCLICK:       m_List.PopupMenu_OnHeaderRClick (_pInfo); break;
			case LVN_COLUMNCLICK: m_List.Sort_OnColumnClick       (_pInfo); break;
		}
		return false;
	}

  private:
	CListView m_List;
};

CRegionsPage::CRegionsPage () : CPropertyPages::CPage (IDD_PA_REGIONS) {}

void CRegionsPage::_UpdateTotalRegions () {
	size_t cRegions = 0;
	for (regions_t::iterator i = g_map.Regions.begin (); i != g_map.Regions.end (); ++ i) {
		const region_t & region = * i;
		if (region.IsDeleted ())
			continue;

		++ cRegions;
	}

	char strTotal [32];
	::sprintf (strTotal, "Total: %d", cRegions);

	SetDlgItemText (IDC_TOTAL, strTotal);
}

bool CRegionsPage::OnInit (HWND _hwndFocus) {
	EnableDlgItem (IDC_REMOVE, false);
	EnableDlgItem (IDC_RENAME, false);

	// Regions.
	m_List.Init (m_hWnd, IDC_LIST, "Regions");
	InitRegionsList (m_List);

	return true;
}

void CRegionsPage::OnDestroy () {
	m_List.Destroy ();
}

void CRegionsPage::OnCommand (WORD _wCmd, WORD _wNotifyCode, HWND _hwndCtl) {
	switch (_wCmd) {
		case IDC_ADD: {
			if (g_map.Countries.empty ()) {
				WarningMsgBox ("Please create countries first.");
				break;
			}

			CCreateRegion crd;
			if (crd.ShowModal (m_hWnd) == 0) {
				FillRegionsList (m_List, & g_map.Regions.back ());
				_UpdateTotalRegions ();
			}

			return;
		}

		case IDC_RENAME: {
			const size_t cSel = ListView_GetSelectionMark (m_List);
			if (cSel == -1)
				break;

			region_t * const pRegion = reinterpret_cast<region_t *> (GetLParam (m_List, cSel));

			string_t strName       = pRegion->strName;
			string_t strAbbrevName = pRegion->strAbbrevName;
			if (EditPAName (m_hWnd, strName, strAbbrevName, "Rename region/province")) {
				RenamePAI (pRegion, strName, strAbbrevName);

				SetItemText    (m_List, cSel, "%s", strName.c_str ());
				SetSubItemText (m_List, cSel, 1, "%s", strAbbrevName.c_str ());
			}

			return;
		}

		case IDC_REMOVE: {
			EnableDlgItem (IDC_RENAME, false);
			EnableDlgItem (IDC_REMOVE, false);

			const size_t cSel = ListView_GetSelectionMark (m_List);
			if (cSel == -1)
				break;

			region_t * const pRegion = reinterpret_cast<region_t *> (GetLParam (m_List, cSel));
			DeletePAI (pRegion);
			FillRegionsList (m_List);
			_UpdateTotalRegions ();

			return;
		}
	}
}

bool CRegionsPage::OnNotify (int _idCtrl, NMHDR * _pInfo) {
	switch (_pInfo->code) {
		case NM_RCLICK:
			m_List.PopupMenu_OnHeaderRClick (_pInfo);
			break;

		case LVN_COLUMNCLICK:
			m_List.Sort_OnColumnClick (_pInfo);
			break;

		case PSN_SETACTIVE: {
			const size_t cSel = ListView_GetSelectionMark (m_List);
			region_t * const pRegion = cSel == -1 ? NULL : reinterpret_cast<region_t *> (GetLParam (m_List, cSel));
			FillRegionsList (m_List, pRegion);
			_UpdateTotalRegions ();
		}
		case NM_CLICK:
		case LVN_KEYDOWN: {
			const size_t cSel = ListView_GetSelectionMark (m_List);
			EnableDlgItem (IDC_RENAME, cSel != -1);
			EnableDlgItem (IDC_REMOVE, cSel != -1);
			break;
		}
	}

	return CPage::OnNotify (_idCtrl, _pInfo);
}